package ohos.opengl;

import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.ResourceManager;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Position;
import ohos.media.image.common.ScaleMode;
import ohos.media.image.common.Size;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Bitmap {

    public static PixelMap createScaledBitmap(PixelMap src, int dstWidth, int dstHeight,
                                              boolean filter) {
        PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
        initializationOptions.scaleMode = ScaleMode.FIT_TARGET_SIZE;
        initializationOptions.size = new Size(dstWidth, dstHeight);
        return PixelMap.create(src, initializationOptions);
    }

    public static PixelMap createBitmap(int[] bt, int width, int height, PixelFormat argb8888) {
        PixelMap.InitializationOptions opts = new PixelMap.InitializationOptions();
        opts.size = new Size(width, height);
        opts.pixelFormat = argb8888;
        return PixelMap.create(bt, opts);
    }

    public static PixelMap create(byte[] imageData, PixelMap.InitializationOptions opts) {
        int[] ints = new int[imageData.length / 4];
        for (int i = 0; i < ints.length; i++) {
            ints[i] = (imageData[0] & 0xFF)
                    | ((imageData[1] & 0xFF) << 8)
                    | ((imageData[2] & 0xFF) << 16)
                    | ((imageData[3] & 0xFF) << 24);
        }
        return PixelMap.create(ints, opts);
    }

    public static PixelMap getPixelMapFromResource(ResourceManager resources, int resourceId) {
        InputStream inputStream = null;
        try {
            // 创建图像数据源ImageSource对象
            inputStream = resources.getResource(resourceId);
            ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
            srcOpts.formatHint = "image/jpg";
            ImageSource imageSource = ImageSource.create(inputStream, srcOpts);
            // 设置图片参数
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            return imageSource.createPixelmap(decodingOptions);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static PixelMap decodeFile(String filePath) {
        try {
            FileInputStream fis = new FileInputStream(filePath);
            return getPixelMapFromStream(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int getWidth(PixelMap pm) {
        return pm.getImageInfo().size.width;
    }

    public static int getHeight(PixelMap pm) {
        return pm.getImageInfo().size.height;
    }

    public static PixelFormat getConfig(PixelMap pixelMap) {
        return pixelMap.getImageInfo().pixelFormat;
    }

    public static void writePixel(PixelMap pixelMap, int x, int y, int color) {
        Position p = new Position(x, y);
        pixelMap.writePixel(p, color);
    }


    //protected Bitmap(long nativeImagePixelMap, long nativeAllocBytes) {
    //  super(nativeImagePixelMap, nativeAllocBytes);
    // }

    public static PixelMap create(int mAtlasWidth, int mAtlasHeight, PixelFormat argb8888) {
        PixelMap.InitializationOptions opts = new PixelMap.InitializationOptions();
        opts.size = new Size(mAtlasWidth, mAtlasHeight);
        opts.pixelFormat = argb8888;
        opts.editable = true;
        return PixelMap.create(opts);
    }

    public static PixelMap getPixelMapFromResource(int resourceId, Context context) {
        InputStream inputStream = null;
        try {
            // 创建图像数据源ImageSource对象
            inputStream = context.getResourceManager().getResource(resourceId);
            ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
            srcOpts.formatHint = "image/jpg";
            ImageSource imageSource = ImageSource.create(inputStream, srcOpts);
            // 设置图片参数
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            return imageSource.createPixelmap(decodingOptions);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static PixelMap getPixelMapFromResource(RawFileEntry rawFileEntry, Context context) {
        InputStream inputStream = null;
        try {
            // 创建图像数据源ImageSource对象
            inputStream = rawFileEntry.openRawFile();
            ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
            srcOpts.formatHint = "image/jpg";
            ImageSource imageSource = ImageSource.create(inputStream, srcOpts);
            // 设置图片参数
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            return imageSource.createPixelmap(decodingOptions);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static PixelMap getPixelMapFromStream(InputStream inputStream) {
        try {
            // 创建图像数据源ImageSource对象
            ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
            srcOpts.formatHint = "image/jpg";
            ImageSource imageSource = ImageSource.create(inputStream, srcOpts);
            // 设置图片参数
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            return imageSource.createPixelmap(decodingOptions);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static PixelMap getPixelMapFromStream(InputStream inputStream, PixelMap.InitializationOptions initializationOptions) {
        PixelMap src = getPixelMapFromStream(inputStream);
        return PixelMap.create(src, initializationOptions);
    }

    public static PixelMap getPixelMapFromStream(InputStream inputStream, ImageSource.DecodingOptions decodingOptions) {
        try {
            // 创建图像数据源ImageSource对象
            ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
            srcOpts.formatHint = "image/jpg";
            ImageSource imageSource = ImageSource.create(inputStream, srcOpts);
            // 设置图片参数
//      ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
//      decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            return imageSource.createPixelmap(decodingOptions);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static PixelMap create(byte[] imageData) {
        ImageSource imageSource = ImageSource.create(imageData, 0, imageData.length, null);
        return imageSource.createPixelmap(null);
    }
}
